package com.xysq.activity;
/**
 * Copyright (c) 2015 XiaoYuanShenQi. All rights reserved.
 * <p/>
 * Author: Steward_Han
 * Date: 2015-6-23
 */

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.rockcent.action.CallbackListener;
import com.rockcent.model.CustomerBO;
import com.rockcent.model.CustomerIncomeBO;
import com.umeng.analytics.MobclickAgent;
import com.xysq.XYApplication;
import com.xysq.lemon.R;
import com.xysq.util.PropertiesUtil;
import com.xysq.util.ToastUtil;
import com.xysq.util.UseUtil;
import com.xysq.util.UserInfoKeeper;
import com.xysq.widget.CircleImageView;
import com.xysq.widget.EditDialog;

import butterknife.InjectView;

public class InfoActivity extends BaseActivity implements View.OnClickListener {

    long exitTime = 0;
    private String phone;
    public final static int RESULT_CODE = 0;
    private boolean isFirst = true;

    @InjectView(R.id.btn_recharge)
    Button rechangeBtn;
    @InjectView(R.id.iv_pic)
    CircleImageView picIv;
    @InjectView(R.id.layout_my_ticket_bag)
    RelativeLayout myTicketBagLayout;
    @InjectView(R.id.layout_my_baby)
    RelativeLayout myBabyLayout;
    @InjectView(R.id.layout_my_skill)
    RelativeLayout mySkillLayout;
    /*    @InjectView(R.id.layout_my_buy_order)
        RelativeLayout myBuyOrderLayout;*/
    @InjectView(R.id.txt_customer_name)
    TextView nameTxt;
    @InjectView(R.id.txt_school)
    TextView schoolTxt;
    @InjectView(R.id.layout_no_login)
    LinearLayout noLoginLayout;
    @InjectView(R.id.layout_login)
    RelativeLayout loginLayout;
    @InjectView(R.id.btn_login)
    Button loginBtn;
    @InjectView(R.id.txt_draw_money)
    TextView drawMoneyTxt;
    @InjectView(R.id.pro_loading)
    ProgressBar loadingPro;
    @InjectView(R.id.txt_income_today)
    TextView incomeTodayTxt;
    @InjectView(R.id.txt_income)
    TextView incomeTxt;
    @InjectView(R.id.txt_withdraw)
    TextView withdrawTxt;
    @InjectView(R.id.layout_about_us)
    RelativeLayout aboutUsLayout;
    @InjectView(R.id.txt_user_phone)
    TextView userPhoneTxt;
    @InjectView(R.id.iv_sex)
    ImageView sexIv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_info);
        initViewAndData();
    }

    public void toSettings() {
        Intent intent = new Intent(this, SettingsActivity.class);
        startActivity(intent);
    }

    private void loadData() {
        getCustomer();
    }

    private void getCustomer() {
        appAction.getCustomer(phone, "", "", new CallbackListener<CustomerBO>() {
            @Override
            public void onSuccess(CustomerBO data) {
                String imageUrl = "";
                if (!TextUtils.isEmpty(data.getHeadUrl()) && !data.getHeadUrl().equals("null")) {
                    if (data.getHeadUrl().startsWith("http")) {
                        imageUrl = data.getHeadUrl();
                    } else {
                        imageUrl = PropertiesUtil.getImageServer() + data.getHeadUrl();
                    }
                    XYApplication.getImageLoader()
                            .displayImage(imageUrl, picIv);
                }
                String name = UseUtil.selectName(data.getNickname(), data.getName(), data.getPhone());
                nameTxt.setText(name);
                userPhoneTxt.setText(data.getPhone());
                schoolTxt.setText(data.getSchoolName());
                if(TextUtils.isEmpty(data.getSex())) {
                    sexIv.setImageResource(R.drawable.ic_nan);
                } else if(data.getSex().equals("2")) {//2代表女
                    sexIv.setImageResource(R.drawable.ic_nv);
                } else {
                    sexIv.setImageResource(R.drawable.ic_nan);
                }
                loadingPro.setVisibility(View.GONE);
                getIncome();
            }

            @Override
            public void onFailure(String errorCode, String message) {
                ToastUtil.showShort(InfoActivity.this, message);
                loadingPro.setVisibility(View.GONE);
            }
        });
    }

    //获取用户收入
    private void getIncome() {
        appAction.queryXyIncome(UserInfoKeeper.readKcouponId(this), new CallbackListener<CustomerIncomeBO>() {
            @Override
            public void onSuccess(CustomerIncomeBO data) {
                incomeTodayTxt.setText(UseUtil.handlePrice(data.getTodayIncome()) + getResources().getString(R.string.txt_yuan));
                incomeTxt.setText(UseUtil.handlePrice(data.getTotalIncome()) + getResources().getString(R.string.txt_yuan));
                drawMoneyTxt.setText(UseUtil.handlePrice(data.getWithdrawCash()) + getResources().getString(R.string.txt_yuan));
            }

            @Override
            public void onFailure(String errorCode, String message) {
                ToastUtil.showShort(InfoActivity.this, message);
            }
        });
    }

    private void initViewAndData() {
        withdrawTxt.setOnClickListener(this);
        rechangeBtn.setOnClickListener(this);
        picIv.setOnClickListener(this);
        myTicketBagLayout.setOnClickListener(this);
        myBabyLayout.setOnClickListener(this);
        mySkillLayout.setOnClickListener(this);
        aboutUsLayout.setOnClickListener(this);
//        myBuyOrderLayout.setOnClickListener(this);
        loginBtn.setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("个人信息页");
        MobclickAgent.onResume(this);
        phone = UserInfoKeeper.readPhoneNumber(this);
        if (!isFirst) {
            loginLayout.setVisibility(View.GONE);
            noLoginLayout.setVisibility(View.VISIBLE);
            loadingPro.setVisibility(View.GONE);
        } else if (TextUtils.isEmpty(phone)) { //未登录，跳到登录页面
            startActivityForResult(new Intent(InfoActivity.this, LoginActivity.class), RESULT_CODE);
            return;
        } else {
            loginLayout.setVisibility(View.VISIBLE);
            noLoginLayout.setVisibility(View.GONE);
            loadingPro.setVisibility(View.VISIBLE);
            loadData();
        }
        isFirst = true;
    }

    @Override
    public void onClick(View view) {
        if (view == withdrawTxt) {
            View inflate = View.inflate(InfoActivity.this, R.layout.dialog_withdraw, null);
            EditDialog editDialog = new EditDialog(InfoActivity.this, inflate);
            editDialog.show();
        }
        if (view == rechangeBtn) {
            startActivity(new Intent(InfoActivity.this, RechargeActivity.class));
        }
        if (view == picIv) {
            Intent intent = new Intent(this, UserActivity.class);
            intent.putExtra(UserActivity.INDEX,"user");
            startActivity(intent);
//            startActivity(new Intent(this,IdentifyActivity.class));
        }
        if (view == myTicketBagLayout) {
            MobclickAgent.onEvent(this, "1016");
            if(TextUtils.isEmpty(phone)) {
                startActivity(new Intent(this,LoginActivity.class));
            } else {
                startActivity(new Intent(this, MyTicketsActivity.class));
            }
        }
        if (view == myBabyLayout) {
            MobclickAgent.onEvent(this, "1015");
            if(TextUtils.isEmpty(phone)) {
                startActivity(new Intent(this,LoginActivity.class));
            } else {
                startActivity(new Intent(this, MyPublishedActivity.class));
            }
        }
        if (view == mySkillLayout) {
            MobclickAgent.onEvent(this, "1017");
            ToastUtil.showShort(this, getResources().getString(R.string.toast_wait_for_online));
//            startActivity(new Intent(this, MyReceiveActivity.class));
        }
        if (view == aboutUsLayout) {
            toSettings();
        }
/*        if (view == myBuyOrderLayout) {
            MobclickAgent.onEvent(this, "1018");
            startActivity(new Intent(this, MyOrderActivity.class));
        }*/
        if (view == loginBtn) {
            startActivityForResult(new Intent(this, LoginActivity.class), RESULT_CODE);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {  //System.currentTimeMillis()无论何时调用，肯定大于2000
                ToastUtil.showLong(InfoActivity.this, getResources().getString(R.string.toast_more_time_exit));
                exitTime = System.currentTimeMillis();
            } else {
                finish();
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == LoginActivity.RESULT_CODE) {
            isFirst = true;
        } else if (requestCode == RESULT_CODE) {
            isFirst = false;
        } else {
            phone = UserInfoKeeper.readPhoneNumber(this);
            if (TextUtils.isEmpty(phone)) {
                loadingPro.setVisibility(View.GONE);
                noLoginLayout.setVisibility(View.VISIBLE);
                loginLayout.setVisibility(View.GONE);
            } else {
                loginLayout.setVisibility(View.VISIBLE);
                noLoginLayout.setVisibility(View.GONE);
                loadData();
            }
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("个人信息页");
        MobclickAgent.onPause(this);
    }
}
